package lz.ls16_ui_objectanimator;

import android.animation.ArgbEvaluator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.button1);
        // startAnimation( iv);
    }

    public void startAnimation(View v) {
        //	iv.setTranslationX(100);
//只要view里面有setXXX()方法就可以通过反射达到变化的目的
        // TODO: 18/3/19  PropertyValuesHolder ->Property ->通过反射调用
/*
           ObjectAnimator oa = ObjectAnimator.ofFloat(v, "translationX", 0f,200f);
        ObjectAnimator oa = ObjectAnimator.ofInt(iv, "backgroundColor", Color.RED, Color.BLUE);
        oa.setDuration(500);
        oa.start();

*/




        //2.---------todo----多个动画同时执行----------------------
        //todo 方法 1) 设置动画监听，同步操作其他的属性
/*
		ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", 0f,100f);
		animator.setDuration(300);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				// 监听动画回调
//				animation.getAnimatedFraction();//动画执行的百分比 0~1 //API 12+
				float value = (float) animation.getAnimatedValue();//得到0f~100f当中的这个时间点对应的值
				iv.setScaleX(0.5f+value/200);
				iv.setScaleY(0.5f+value/200);
//				iv.setTranslationX(value);
			}
		});
		animator.start();
*/


        // TODO:  方法 2）---------------ValueAnimator---如果只需要监听值变化就用ValueAnimator---------------
		/* ValueAnimator animator = ValueAnimator.ofFloat(0f, 100f);
		animator.setDuration(200);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animation) {

				float value = (float) animation.getAnimatedValue();//得到0f~100f当中的这个时间点对应的值
				iv.setScaleX(0.5f+value/200);
				iv.setScaleY(0.5f+value/200);
			}
		});
		animator.start();*/

        //todo 方法 3) ----- ofPropertyValuesHolder----
	/*	PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f,0.5f);
		PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f,0.5f);
		PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f,0.5f);
		ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(iv, holder1,holder2,holder3);
		animator.setDuration(200);
		animator.start();*/

        //todo 方法 4）-------------动画集合---animatorSet.playSequentially--------------
	/*ObjectAnimator animator1 = ObjectAnimator.ofFloat(iv, "translationX", 0f,100f);
	//	animator1.setRepeatCount(3);
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv, "alpha", 0f,1f);
//		animator2.setStartDelay(startDelay)//设置延迟执行
		ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv, "scaleX", 0f,2f);
		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(500);
		animatorSet.play(animator3).with(animator2).after(animator1);//animator1在前面
//		animatorSet.play(animator3).with(animator2).before(animator1);//animator1在后面
//		animatorSet.playTogether(animator1,animator2,animator3);
//		animatorSet.playSequentially(animator1,animator2,animator3);
		animatorSet.start();
*/

        //todo 5.------------------案例：实现自由落体抛物线效果---购物车动画、股指数-----------------
        /**
         * x: 匀速
         * y: 加速度 y=vt=1/2*g*t*t
         * //todo  估值器---控制坐标PointF(x,y)
         */

        ValueAnimator valueAnimator = new ValueAnimator();
//		valueAnimator.setInterpolator(value)  //插值器
        valueAnimator.setInterpolator(new BounceInterpolator());  //todo 弹簧插值器
        valueAnimator.setDuration(2000);
        valueAnimator.setObjectValues(new PointF(0, 0));
//		valueAnimator.setObjectValues(new PointF(0, 0),new PointF(10, 10));
        final PointF pointF = new PointF();
        //颜色估值器   估值器
//		setBackgroundColor((Integer) sArgbEvaluator.evaluate(ratio, mDiscrollveFromBgColor, mDiscrollveToBgColor));
        valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {

            @Override
            public PointF evaluate(float fraction, PointF startValue,
                                   PointF endValue) {
                // 估值计算方法---可以在执行的过程当中干预改变属性的值---做效果：用自己的算法来控制
                //不断地去计算修改坐标
                //x匀速运动 x=v*t 为了看起来效果好我让t变成fraction*5
                pointF.x = 100f*(fraction*5);
                //加速度 y=vt=1/2*g*t*t
//				pointF.y = 0.5f*9.8f*(fraction*5)*(fraction*5);
                pointF.y = 10f*0.5f*9.8f*(fraction*5)*(fraction*5);
                return pointF;
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF f = (PointF) animation.getAnimatedValue();
                iv.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(animation.getAnimatedFraction(), Color.RED,Color.GREEN));
                iv.setX(f.x);
                iv.setY(f.y);
            }
        });
        valueAnimator.start();





//todo 6.---------插值器（加速器）Interpolater-----------
	/*
	ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationY", 0f,1000f);
		oa.setDuration(800);
 //       TimeInterpolator
		oa.setInterpolator(new AccelerateInterpolator(1));
		oa.setInterpolator(new AccelerateDecelerateInterpolator());
		oa.setInterpolator(new BounceInterpolator());
		oa.setInterpolator(new AnticipateInterpolator());
		oa.setInterpolator(new CycleInterpolator(5));

		oa.start();

		*/
    }



}
